﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.UpdateServices.Administration;
using WsusWebAdmin.Web.Models;
using Telerik.Web.Mvc;
using WsusWebAdmin.Web.Helpers;

namespace WsusWebAdmin.Web.Controllers
{
    public class ComputerTargetGroupController : UpdateServerBaseController
    {
        //
        // GET: /ComputerTargetGroup/

        public ActionResult Details(Guid id)
        {
            var group = UpdateServer.GetComputerTargetGroup(id);
            var summary = group.GetTotalSummary();
            IComputerTargetGroup parentGroup;
            try
            {
                parentGroup = group.GetParentTargetGroup();
            }
            catch (Exception)
            {

                parentGroup = null;
            }
            var childGroups = group.GetChildTargetGroups();

            ViewData.Model = new ComputerTargetGroupViewModel
            {
                Group = group,
                UpdateSummary = summary,
                ParentGroup = parentGroup,
                ChildGroups = childGroups
            };

            return View();
        }

        [GridAction]
        public ActionResult Computers(Guid id)
        {
            var group = UpdateServer.GetComputerTargetGroup(id);
            //List<IComputerTarget> computers = new List<IComputerTarget>();

            //foreach (IComputerTarget item in group.GetComputerTargets(false))
            //{
            //    computers.Add(item);
            //}

            var computers = group.GetComputerTargets(false).AsEnumerable(c => new
            {
                Id = ((IComputerTarget)c).Id,
                LastReportedStatusTime = ((IComputerTarget)c).LastReportedStatusTime,
                FullDomainName = ((IComputerTarget)c).FullDomainName
            });

            return GridView(new GridModel
            {
                Data = computers
            });
        }
        
    }
}
